﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

using Kortex.Common;
using Kortex.AllCommandsSpace;
using Kortex.Communication_objs;
using Kortex.Container;
using System.Drawing;
using sharpSshTest;
using System.Globalization;
using System.Net.Sockets;
using System.Net;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace Kortex.WallMgr.NSL
{

    public enum responceType
    {
        should_send,
        should_not_send,
    
    }

    public enum tftp_srtatus
    {
        in_progress,
        done_with_sucess,
        Done_with_error,
        None,
    }
    public class NSLcube
    {
        public string mIp = string.Empty;
        public string mMacAddress1 = string.Empty;
        public string mMacAddress2 = string.Empty;
        public string mDispSerialNumber = string.Empty;
        public int pos_Col=0, pos_Row=0;
        public int mXmax, mYmax;
        public string wallConfInput = string.Empty;
        public string wallConfMode = string.Empty;
        public string version = "0.1";
        private string localDir;
        List<Cube_command_reqObj> req_chain = new List<Cube_command_reqObj>();
        Dictionary<int, Cube_command_reqObj> requested_chain = new Dictionary<int, Cube_command_reqObj>();
        bool run = false;
        WallManager WallMgr = null;
        Thread Worker_req = null;
        private CommandHandleKP AllCommands = null;
        bool IsConnected = false;
        string lastSlectedInput = string.Empty;
        //private string fmwUpdateRpcId;
        string mFmwVersion = string.Empty;
        bool Optimiz = true;

        tftp_srtatus tftP_state = tftp_srtatus.None;
        //private List<ReqRespDebugInfo> DebugReRes = new List<ReqRespDebugInfo>();

        public CommandHandleKP pubAllCommands
        {
            set
            {
                AllCommands = value;
            }

        }

        public bool isConnected
        {
            get 
            {
                return IsConnected;
            }
        
        }

        public NSLcube()
        { 
        
        }
        
        
        public NSLcube(int col_p,int row_p,WallManager wObj,string Local_dir,int Max_xP,int Max_yp)
        {
            pos_Col = col_p;
            pos_Row = row_p;
            WallMgr = wObj;
            localDir = Local_dir;
            wallConfInput = WallMgr.wallConfigInput;
            wallConfMode = WallMgr.wallConfigMode;

            mXmax = Max_xP;
            mYmax = Max_yp;

            Kortex.Factory.Factory.CreateAll(this);

        }

        public NSLcube(NSLcube obj,WallManager wallObj)
        {
            mIp = obj.mIp;
            mMacAddress1 = obj.mMacAddress1;
            mMacAddress2 = obj.mMacAddress2;
            
            pos_Col = obj.pos_Col;
            pos_Row = obj.pos_Row;
            WallMgr = wallObj;
            localDir = obj.localDir;
            wallConfInput = obj.wallConfInput;
            wallConfMode = obj.wallConfMode;

            mXmax = obj.mXmax;
            mYmax = obj.mYmax;

            Kortex.Factory.Factory.CreateAll(this);

        }

        #region Responce Mgr

        public void UpdateDebugInfo(ReqRespDebugInfo json)
        {
           // Monitor.Enter(DebugReRes);

            json.mIp = mIp;
            json.mCol = pos_Col;
            json.mRow = pos_Row;
           // DebugReRes.Add(json);
           // MessageBox.Show(json.mJson);

            WallMgr.RawTransction(json);
           // Monitor.Exit(DebugReRes);

        
        }

        public bool iSAnyRequestPending()
        {

            if (requested_chain.Count > 0) 
                return true;


            return false;
        }
        private responceType GetUpdateResult(Cube_command_reqObj req, General_responce res, string rpcID)
        {
            int id;

            

            if (res.result.taskStatus != null)
                if (res.result.taskStatus.ToLower() == "success")
                {
                   //Gen_Fmw_updare_result_show.Text = res.result.taskStatus;
                   // Gen_Fmw_updare_result_show.BackColor = Color.GreenYellow;
                    //WallMgr.Responce(req, res);

                    return responceType.should_send;
                }
               // else
                    //Gen_Fmw_updare_result_show.Text = res.result.taskStatus;

            if (res.result.taskStatus != null)
                if (res.result.taskStatus.ToLower() == "taskidnotfound" || res.result.taskStatus.ToLower() == "fail") return  responceType.should_send;

           // MessageBox.Show(res.result.taskStatus);
           // Thread.Sleep(2000);
            //Gen_Fmw_updare_result_show.BackColor = Color.HotPink;  cmd_fmw_update_status_get
            Cube_command_reqObj obj = new Cube_command_reqObj(1, pos_Col, pos_Row, Common.commandType.cmd_fmw_update_status_get, null);
            Monitor.Enter(requested_chain);
            id = AllCommands.Getfmw_update_status(rpcID);
            requested_chain[id] = req;
            Monitor.Exit(requested_chain);

            return  responceType.should_not_send;
           

        }

        private void handleWallCongfig_forSlectedInputAndMode(string input_p)
        {
            int id;


            lastSlectedInput = input_p;
            Cube_command_reqObj obj = new Cube_command_reqObj(1, pos_Col, pos_Row, Common.commandType.cmd_img_wallGeometry_get_internal, null);
            Monitor.Enter(requested_chain);
            id = AllCommands.GetWallGeometry(input_p);
            requested_chain[id] = obj;
            Monitor.Exit(requested_chain);
    
        }

        private void handleWallCongfig_forFixInputAndSlectedMode(string input_p)
        {
            int id;


            lastSlectedInput = input_p;
            Cube_command_reqObj obj = new Cube_command_reqObj(1, pos_Col, pos_Row, Common.commandType.cmd_img_wallGeometry_get_internal, null);
            Monitor.Enter(requested_chain);
            id = AllCommands.GetWallGeometry(input_p);
            requested_chain[id] = obj;
            Monitor.Exit(requested_chain);

        }

        private void SetWallConfig(string input,string mode)
        {
            int id;
            Cube_command_reqObj obj = new Cube_command_reqObj(1, pos_Col, pos_Row, commandType.cmd_img_wallConfig_set_internal, null);
            Monitor.Enter(requested_chain);
            id = AllCommands.SetWallGeometry(pos_Col+1, pos_Row+1, mXmax, mYmax, input, mode);
            requested_chain[id] = obj;
            Monitor.Exit(requested_chain);
        
        }

        private void GetTftpResult(Cube_command_reqObj req, General_responce res, string rpcID)
        {
            int id;
            if (rpcID != null)
                req.rpcPollId = rpcID;

            if (res.result.taskStatus != null)
                if (res.result.taskStatus.ToLower() == "success")
                {

                    tftP_state = tftp_srtatus.done_with_sucess;
                    //ALLCubes[req.mCol, req.mRow].delete_fmw_file();
                    return;
                }


            if (res.result.taskStatus != null)
                if (res.result.taskStatus.ToLower() == "taskidnotfound" || res.result.taskStatus.ToLower() == "fail")
                {

                    tftP_state = tftp_srtatus.Done_with_error;
                    //ALLCubes[req.mCol, req.mRow].delete_fmw_file();
                    return;
                }

            Thread.Sleep(2000);

           
            
            Cube_command_reqObj obj = new Cube_command_reqObj(1, pos_Col, pos_Row, Common.commandType.cmd_do_spi_rawtftp_done_status, null);
            Monitor.Enter(requested_chain);
           // id = AllCommands.UploadFmw_viaTftp(req_obj.args[3], "/tmp/RawSpi.bin", "RawSpi.bin");
            id = AllCommands.Getfmw_update_status(req.rpcPollId);
            requested_chain[id] = obj;
            Monitor.Exit(requested_chain);


            obj.rpcPollId = req.rpcPollId; // because now this time i will get this object as requested object

           // ALLCubes[req.mCol, req.mRow].Request(obj);



        }

        private double CheckFmwVersion()
        {
            string value = Regex.Replace(mFmwVersion, "[A-Za-z ]", "");
            string[] parts = value.Split('.');
            double version = Convert.ToDouble(parts[0] + "." + parts[1]);
            return version;
        }

        private void ConnectRemainingServices(double ver)
        {
            ConnectCubes(Optimiz);


        }
        private void Apply_version_policy_at_connect(double ver)
        {
            if (ver >= 1.0)
            {
                ConnectRemainingServices(ver);
                return;
            }

             MessageBox.Show("Old Firmware found!! at cube Ip ="+ mIp + "POS="+(pos_Col+1)+","+(pos_Row+1) + "With this version of GUI you can only upgrade this cube to latest.", "ERROR!!", MessageBoxButtons.OK, MessageBoxIcon.Error);


        }
        private responceType handle_result(Cube_command_reqObj req, General_responce res)
        {
           
            switch (req.cmdType)
            {
                //cmd_mrc_spi_module_version_at_connect_get
                case commandType.cmd_mrc_spi_module_version_at_connect_get:
                    if (res.result.version != null)
                    {
                        
                        mFmwVersion = res.result.version;
                        Apply_version_policy_at_connect(CheckFmwVersion());
                    }
                    break;
                case commandType.cmd_do_spi_rawtftp:
                case commandType.cmd_do_spi_rawtftp_done_status:
                    GetTftpResult(req, res, res.result.taskId);

                    break;
                case commandType.cmd_img_wallGeometry_get_internal:
                    if (res.result != null)
                    {
                        if (wallConfInput == "Selected input" && wallConfMode == "Selected mode")
                            SetWallConfig(lastSlectedInput, res.result.mode);
                        else if (wallConfInput != "Selected input" && wallConfMode == "Selected mode")
                            SetWallConfig(wallConfInput, res.result.mode);
                        return responceType.should_not_send;
                    }
                    return responceType.should_not_send;
                    
                case commandType.cmd_get_input_selected_internal:
                   // if (res.result == null) 
                     //   return responceType.should_not_send;
                    switch (res.result.input)
                    {
                        case "dvi1":
                        case "dvi2":
                        case "hdmi":
                        case "ops" :
                        case "dp"  :
                        case "onboard":
                            if (wallConfInput == "Selected input" && wallConfMode == "Selected mode")
                                handleWallCongfig_forSlectedInputAndMode(res.result.input);
                            else if (wallConfInput == "Selected input" && wallConfMode != "Selected mode")
                                SetWallConfig(res.result.input, wallConfMode);
                            else if (wallConfInput != "Selected input" && wallConfMode == "Selected mode")
                                handleWallCongfig_forFixInputAndSlectedMode(wallConfInput);
                            else if (wallConfInput != "Selected input" && wallConfMode != "Selected mode")
                                SetWallConfig(wallConfInput, wallConfMode);

                            break;
                        default:
                            //MessageBox.Show("For Cube ip = "+ mIp +" selected  input is " + res.result.input + " and wall config can not apply for this input");
                            handleWallCongfig_forSlectedInputAndMode("dp");
                            return responceType.should_not_send;

                    
                    }

                    break;
                case commandType.cmd_mac1_address_get_internal:
                   if (res.result.mac != null)
                    {
                        //string[] str = res.result.macaddr.Split('=');
                        mMacAddress1 = res.result.mac;
                    }

                   return responceType.should_not_send;
                case commandType.cmd_bid_Disp_serial_number_get_internal:

                   if (res.result.value != null)
                    {
                        //string[] str = res.result.macaddr.Split('=');
                        //mMacAddress2 = str[1].Replace("\n", "");
                        mDispSerialNumber = res.result.value;
                    }
                   break;
                case commandType.cmd_mac2_address_get_internal:
                   if (res.result.mac != null)
                    {
                        //string[] str = res.result.macaddr.Split('=');
                        //mMacAddress2 = str[1].Replace("\n", "");
                        mMacAddress2 = res.result.mac;
                    }


                    return responceType.should_not_send;

                /*case commandType.cmd_fmw_update_spi:
                case commandType.cmd_fmw_update_ps171:
                case commandType.cmd_fmw_update_mstar:
                case commandType.cmd_fmw_update_mmc:
                    fmwUpdateRpcId = res.result.taskId;
                    if (res.result.taskId != null)
                        return GetUpdateResult(req, res, res.result.taskId);
                    // = res.result.taskId;
                    break;
                case commandType.cmd_fmw_update_status_get:
                    return GetUpdateResult(req, res, fmwUpdateRpcId);

                    break;
                 * */
                
                
                    
            }

            return responceType.should_send;

        }
        public void Responce(General_responce res)
        {

            responceType ret = responceType.should_send;
            Cube_command_reqObj req_obj = null ;
            if (res == null) return;

            
            Monitor.Enter(requested_chain);
            try
            {
                 req_obj = requested_chain[res.id];
            }
            catch(Exception er)
            {
                Monitor.Exit(requested_chain);
                return;
            }
            if (req_obj == null) return;
            requested_chain.Remove(res.id);
            Monitor.Exit(requested_chain);
            //Few Imp datas to be copied into req object
            req_obj.mIp = mIp;
            req_obj.mSerialNum = mDispSerialNumber;
            
            req_obj.ident = res.id;
            req_obj.mCol = pos_Col;
            req_obj.mRow = pos_Row;

            if (res.result != null)
                ret = handle_result(req_obj, res);

            if (req_obj.requester_guy != cmd_requster.nslcubeself)
                WallMgr.Responce(req_obj, res);
            else
            {
                 Monitor.Enter(requested_chain);
                requested_chain[res.id] = req_obj;
                Monitor.Exit(requested_chain);
            
            }

            
             
        }

        #endregion

    public void set_ip(string ip_p)
        {
            mIp = ip_p;
        }


        public void Unset_ip()
        {
            mIp = string.Empty;
        }



        private void init_wallConfig()
        {
            //wallConfigInput

            if (wallConfInput == "none" || wallConfInput == string.Empty) //Do not apply wall config if user selected none
                return;

            //if (wallConfInput == "Selected input" || wallConfMode == "Selected mode") //Apply wall config on Selected Input or Seleted Mode
            {
                int id;
                Cube_command_reqObj obj = new Cube_command_reqObj(cmd_requster.nslcubeself, pos_Col, pos_Row, commandType.cmd_get_input_selected_internal, null, null, null);
                Monitor.Enter(requested_chain);
                id = AllCommands.GetMercurySelectedSource();
                requested_chain[id] = obj;
                Monitor.Exit(requested_chain);
                return;
            }

            //handleWallCongfig_forInput(wallConfInput); //Apply wall config on user Selected Input
        
        }

        public void ApplyWallConfig()
        {
            if (mIp != string.Empty && IsConnected)
            init_wallConfig();
        }

        public void ConnectAny()
        {
            int id;
            Cube_command_reqObj obj = null;

            if (mIp != string.Empty && mIp.Contains(".") && !IsConnected)
            {
                try
                {
                    AllCommands.ConnectTosdPanelService(mIp, 42511);
                   
                }
                catch (Exception er)
                {
                    //IsConnected = false;
                    MessageBox.Show("Error While Connect at Cube ip=" + mIp + (pos_Col+1) + "," + (pos_Row+1) + "  Message= " + er.Message);
                }

                /*try
                {
                   
                    AllCommands.ConnectTosdFpgaService(mIp, 42512);
                    
                }
                catch (Exception er)
                {
                    //IsConnected = false;
                    MessageBox.Show("Error While Connect at Cube " + pos_Col + "," + pos_Row + "  Message= " + er.Message);
                }
                 * */

                try
                {
                    AllCommands.ConnectTosdSysMgrService(mIp, 42513);
                    
                }
                catch (Exception er)
                {
                   // IsConnected = false;
                    MessageBox.Show("Error While Connect at Cube ip=" + mIp + (pos_Col + 1) + "," + (pos_Row + 1) + "  Message= " + er.Message);
                }

                try
                {
                    AllCommands.ConnectTosdInputService(mIp, 42514);
                    
                }
                catch (Exception er)
                {
                   // IsConnected = false;
                    MessageBox.Show("Error While Connect at Cube ip=" + mIp + (pos_Col + 1) + "," + (pos_Row + 1) + "  Message= " + er.Message);
                }

                try
                {
                    AllCommands.ConnectTosdColorService(mIp, 42516);
                }
                catch (Exception er)
                {
                    //IsConnected = false;
                    MessageBox.Show("Error While Connect at Cube ip=" + mIp + (pos_Col + 1) + "," + (pos_Row + 1) + "  Message= " + er.Message);
                }
                   

                    Monitor.Enter(requested_chain);
                    obj = new Cube_command_reqObj(cmd_requster.nslcubeself, pos_Col, pos_Row, commandType.cmd_mac1_address_get_internal, null, null, null);
                    id = AllCommands.GetMacAddress("eth0");
                    requested_chain[id] = obj;

                    obj = new Cube_command_reqObj(cmd_requster.nslcubeself, pos_Col, pos_Row, commandType.cmd_mac2_address_get_internal, null, null, null);

                    id = AllCommands.GetMacAddress("eth1");
                    requested_chain[id] = obj;

                    Monitor.Exit(requested_chain);



                    run = true;
                    Worker_req = new Thread(req_Worker);
                    Worker_req.Name = "Kp Cube Request Worker For " + mIp;
                    Worker_req.Start();

                    IsConnected = true;
                }
                

                if (IsConnected)
                    ApplyWallConfig();
            }




        public void Pro_rev_responce()
        {

            AllCommands.Proc_rev_responces();
        }

        


        public void Connect()
        {
            int id;
            Cube_command_reqObj obj = null;
            
            if (mIp != string.Empty && mIp.Contains(".") && !IsConnected)
            {
                try
                {
                    AllCommands.ConnectTosdPanelService(mIp, 42511);
                    //AllCommands.ConnectTosdFpgaService(mIp, 42512);
                    AllCommands.ConnectTosdSysMgrService(mIp, 42513);
                    AllCommands.ConnectTosdInputService(mIp, 42514);
                    //AllCommands.ConnectTosdColorService(mIp, 42516);
                    AllCommands.ConnectTosdColorService(mIp, 42516);

                    /*
                    Cube_command_reqObj obj = new Cube_command_reqObj(1, pos_Col, pos_Row, commandType.cmd_img_wallConfig_set, null);
                    Monitor.Enter(requested_chain);
                    id = AllCommands.SetWallGeometry(pos_Col, pos_Row, mXmax, mYmax);
                    requested_chain[id] = obj;
                    */
                    Monitor.Enter(requested_chain);
                    obj = new Cube_command_reqObj(cmd_requster.nslcubeself , pos_Col, pos_Row, commandType.cmd_mac1_address_get_internal, null,null,null);
                    //id = AllCommands.GetMacAddress("eth0");
                    id = AllCommands.GetNetInfo("eth0");
                    requested_chain[id] = obj;

                    obj = new Cube_command_reqObj(cmd_requster.nslcubeself, pos_Col, pos_Row, commandType.cmd_mac2_address_get_internal, null, null, null);
                    
                    //id = AllCommands.GetMacAddress("eth1");
                    id = AllCommands.GetNetInfo("eth1");
                    requested_chain[id] = obj;

                    Monitor.Exit(requested_chain);

                   

                    run = true;
                    Worker_req = new Thread(req_Worker);
                    Worker_req.Name = "Kp Cube Request Worker For " + mIp ;
                    Worker_req.Start();

                    IsConnected = true;
                }
                catch(Exception er)
                {
                    FourceDisconnect();
                    MessageBox.Show("Error While Connect at Cube ip=" + mIp + (pos_Col + 1) + "," + (pos_Row + 1) + "  Message= " + er.Message);
                }

                if (IsConnected)
                 ApplyWallConfig();
            }
            //else
              // MessageBox.Show("NO Ip has been set for cube row=" + pos_Row.ToString() + "Col=" + pos_Col.ToString(), "BABA ji ka thullu abe Pahale IP to set kar", MessageBoxButtons.OK, MessageBoxIcon.Error);

           
            
        
        }
        public void Connect(bool Optimize)
        {
            int id;
           

            if (mIp != string.Empty && mIp.Contains(".") && !IsConnected)
            {
                try
                {
                    
                    AllCommands.ConnectTosdSysMgrService(mIp, 42513, Optimize);

                    Cube_command_reqObj req = new Cube_command_reqObj(cmd_requster.nslcubeself, 1, pos_Col, pos_Row, commandType.cmd_mrc_spi_module_version_at_connect_get, null, null, null);
                   // Request(req);
                    handle_get_fmw_version(req);
                    
                    run = true;
                    Worker_req = new Thread(req_Worker);
                    Worker_req.Name = "Kp Cube Request Worker For " + mIp;
                    Worker_req.Start();
                    
                    IsConnected = true;
                   

                    //IsConnected = true;
                }
                catch (Exception er)
                {
                    FourceDisconnect();
                    MessageBox.Show("Error While Connect at Cube ip=" + mIp + (pos_Col + 1) + "," + (pos_Row + 1) + "  Message= " + er.Message);

                }

               
            }
            //else
            // MessageBox.Show("NO Ip has been set for cube row=" + pos_Row.ToString() + "Col=" + pos_Col.ToString(), "BABA ji ka thullu abe Pahale IP to set kar", MessageBoxButtons.OK, MessageBoxIcon.Error);




        }


        public void ConnectCubes(bool Optimize)
        {
            int id;
            Cube_command_reqObj obj = null;

            //if (mIp != string.Empty && mIp.Contains(".") && !IsConnected)
            {
                try
                {
                    AllCommands.ConnectTosdPanelService(mIp, 42511, Optimize);
                    AllCommands.ConnectTosdFpgaService(mIp, 42512);
                   // AllCommands.ConnectTosdSysMgrService(mIp, 42513, Optimize);
                    AllCommands.ConnectTosdInputService(mIp, 42514, Optimize);
                    //AllCommands.ConnectTosdColorService(mIp, 42516);
                    AllCommands.ConnectTosdColorService(mIp, 42516, Optimize);

                    /*
                    Cube_command_reqObj obj = new Cube_command_reqObj(1, pos_Col, pos_Row, commandType.cmd_img_wallConfig_set, null);
                    Monitor.Enter(requested_chain);
                    id = AllCommands.SetWallGeometry(pos_Col, pos_Row, mXmax, mYmax);
                    requested_chain[id] = obj;
                    */
                    Monitor.Enter(requested_chain);
                    obj = new Cube_command_reqObj(cmd_requster.nslcubeself, pos_Col, pos_Row, commandType.cmd_mac1_address_get_internal, null, null, null);
                    //id = AllCommands.GetMacAddress("eth0");
                    id = AllCommands.GetNetInfo("eth0");
                    requested_chain[id] = obj;

                    obj = new Cube_command_reqObj(cmd_requster.nslcubeself, pos_Col, pos_Row, commandType.cmd_mac2_address_get_internal, null, null, null);

                    //id = AllCommands.GetMacAddress("eth1");
                    id = AllCommands.GetNetInfo("eth1");
                    requested_chain[id] = obj;

                    obj = new Cube_command_reqObj(cmd_requster.nslcubeself, pos_Col, pos_Row, commandType.cmd_bid_Disp_serial_number_get_internal, null, null, null);

                    //id = AllCommands.GetMacAddress("eth1");
                    id = AllCommands.GetBid("display", "dev_serl_num");
                    requested_chain[id] = obj;



                    Monitor.Exit(requested_chain);

                    /*
                    run = true;
                    Worker_req = new Thread(req_Worker);
                    Worker_req.Name = "Kp Cube Request Worker For " + mIp;
                    Worker_req.Start();
                    */
                    IsConnected = true;
                }
                catch (Exception er)
                {
                    FourceDisconnect();
                    MessageBox.Show("Error While Connect at Cube ip=" + mIp + (pos_Col + 1) + "," + (pos_Row + 1) + "  Message= " + er.Message);
                    
                }

                if (IsConnected)
                    ApplyWallConfig();
            }
            //else
            // MessageBox.Show("NO Ip has been set for cube row=" + pos_Row.ToString() + "Col=" + pos_Col.ToString(), "BABA ji ka thullu abe Pahale IP to set kar", MessageBoxButtons.OK, MessageBoxIcon.Error);




        }


        #region Wakeon Lan
        private void WakeUp(string ip_p, byte[] mac)
        {
            //
            // WOL packet is sent over UDP 255.255.255.0:40000.
            //
            byte[] WOl = new byte[4];


            if (ip_p != "")
            {
                string[] Ip = ip_p.Split(".".ToCharArray());
                for (int i = 0; i < Ip.Length; i++)
                {
                    WOl[i] = byte.Parse(Ip[i], NumberStyles.Number);

                }
            }
            else
            {
                WOl[0] = 0xFF;
                WOl[1] = 0xFF;
                WOl[2] = 0xFF;
                WOl[3] = 0xFF;
            }

            IPAddress IPAddr = new IPAddress(WOl);
            UdpClient client = new UdpClient();
            client.Connect(IPAddr, 40000);

            //
            // WOL packet contains a 6-bytes trailer and 16 times a 6-bytes sequence containing the MAC address.
            //
            byte[] packet = new byte[17 * 6];

            //
            // Trailer of 6 times 0xFF.
            //
            for (int i = 0; i < 6; i++)
                packet[i] = 0xFF;

            //
            // Body of magic packet contains 16 times the MAC address.
            //
            for (int i = 1; i <= 16; i++)
                for (int j = 0; j < 6; j++)
                    packet[i * 6 + j] = mac[j];

            //
            // Submit WOL packet.
            //
            client.Send(packet, packet.Length);
        }

        private void CreateAndSendMagicPkt(string ip_p, string mac_p)
        {
            int i = 0;
            int counter = 0;
            byte[] bytes = new byte[10];   // more than enough :-)
            try
            {
                for (int z = 0; z < 6; z++)
                {
                    bytes[counter++] = byte.Parse(mac_p.Substring(i, 2), NumberStyles.HexNumber);
                    i += 3;
                }

                WakeUp(ip_p, bytes);
            }
            catch (Exception er)
            {
                MessageBox.Show(er.Message);
            }

        }

        private void Brodcast_WOL_Click(object sender, EventArgs e)
        {

            if (mMacAddress1 != string.Empty)
            {
                CreateAndSendMagicPkt(mIp, mMacAddress1);
                CreateAndSendMagicPkt("", mMacAddress1);
            }

            if (mMacAddress2 != string.Empty)
            {
                CreateAndSendMagicPkt(mIp, mMacAddress2);
                CreateAndSendMagicPkt("", mMacAddress2);
            }
        }
        public void wakeOnlan()
        {
            try
            {
                Brodcast_WOL_Click(null, null);
            }
            catch(Exception er)
            { 

            }
        
        }

        #endregion 
        public void Disconnect()
        {
            if (mIp != string.Empty && IsConnected)
            AllCommands.DisConnectFromServr();

            run = false;
            if(Worker_req != null)
            Worker_req.Abort();

            //mIp = string.Empty;
            IsConnected = false;

            requested_chain.Clear();
            req_chain.Clear();

        
        }

        public void FourceDisconnect()
        {
            
                AllCommands.DisConnectFromServr();

            run = false;
            if (Worker_req != null)
                Worker_req.Abort();

            //mIp = string.Empty;
            IsConnected = false;

            requested_chain.Clear();
            req_chain.Clear();


        }

        public int Request(Cube_command_reqObj obj)
        {
            if (Worker_req != null)
            {
                Monitor.Enter(req_chain);
                req_chain.Add(obj);
                Monitor.Exit(req_chain);
            }
            //else
                //MessageBox.Show("Not connect to cube Request Rejected plz connect first","BABA ji ka thullu abe Pahale Connect to kar",MessageBoxButtons.OK,MessageBoxIcon.Error);
            return 0;
        }

        public void req_Worker()
        {
           
            while (run)
            {
               
                if (req_chain.Count > 0)
                {
                   // upperIf.send(responceChain[0]);
                    //Process it
                    if (mIp != string.Empty)
                    {
                        process_req(req_chain[0]);
                        Monitor.Enter(req_chain);
                        req_chain.RemoveAt(0);
                        Monitor.Exit(req_chain);
                       
                    }
                }
                else
                {
                    Thread.Sleep(10);
                }
                

            }
        }
        private string GetDyki()
        {

            char[] Data = new char[11];

            Data[0] = (char)0x42;
            Data[1] = (char)0x61;
            Data[2] = (char)0x72;
            Data[3] = (char)0x63;
            Data[4] = (char)0x6F;
            Data[5] = (char)0x23;
            Data[6] = (char)0x31;
            Data[7] = (char)0x32;
            Data[8] = (char)0x38;
            Data[9] = (char)0x2F;
            Data[10] = (char)0x35;

            return new string(Data);
        }

        public void delete_fmw_file()
        {
             string dyKI = GetDyki();
             KpScpSshclt.kp_runCmd(mIp, "rm -rf /tmp/fmwUpdate.bin", dyKI);
 
        
        }

        private string  DoScp(Cube_command_reqObj req_obj)
        {
            bool keepTry = true;
            string dyKI = GetDyki();
            string filePath = "/tmp/fmwUpdate.bin";

            while (keepTry)
            {

                try
                {
                    keepTry = false;
                    if (CheckFmwVersion() >= 0.32)
                        KpScpSshclt.kp_scp(mIp, req_obj.args[1], filePath, dyKI);
                    else
                        KpScpSshclt.kp_scp_root(mIp, req_obj.args[1], filePath, dyKI);

                }
                catch (Exception er)
                {
                    DialogResult ret = MessageBox.Show("SCP for IP =" + mIp + " Pos =" + pos_Col + "," + pos_Row + " Failed due to " + er.Message + " Do you wish to try again", "ERROR!!", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                    if (ret == DialogResult.Yes)
                    {
                        delete_fmw_file();
                        keepTry = true;
                    }
                    else
                        return string.Empty;
                }

            }

            return filePath;
        
        }

        private string DoCgi(Cube_command_reqObj req_obj)
        {
            string cmd = string.Empty;
            string exe = string.Empty;
            bool ret;

            exe = localDir + "\\curl.exe" ;
            //cmd = " --output d:\\Curl_Result.txt -v -F \"json=@" + req_obj.args[1]+"\"" + " -F \"type=application/octet-stream\" -H \"Expect:\" http://"+ mIp + "/cgi-bin/upload" ;
            cmd = "  -F \"json=@" + req_obj.args[1] + "\"" + " -F \"type=application/octet-stream\" -H \"Expect:\" http://" + mIp + "/cgi-bin/upload";

            System.Diagnostics.ProcessStartInfo Input = new ProcessStartInfo(@exe, cmd);
                Input.CreateNoWindow = true;
                Input.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                System.Diagnostics.Process kp = System.Diagnostics.Process.Start(Input);
            
            
            ret = kp.WaitForExit(2500000);


            
            string []RealFile = req_obj.args[1].Split("\\".ToCharArray());
            
            if (ret)
                return "/tmp/fmw_update/" + RealFile[RealFile.Length-1];
            else
                return string.Empty;

        }

        private bool waitForTftpDone()
        {
            while (tftP_state == tftp_srtatus.in_progress)
            {
                Thread.Sleep(1000);
            }

            if (tftP_state == tftp_srtatus.done_with_sucess)
                return true;
            else
                return false;
        
        }

        private string Dotftp(Cube_command_reqObj req_obj)
        {

            int id;
            bool ret = false;
            Cube_command_reqObj obj = new Cube_command_reqObj(1, pos_Col, pos_Row, Common.commandType.cmd_do_spi_rawtftp, null);
            Monitor.Enter(requested_chain);
            id = AllCommands.UploadFmw_viaTftp(req_obj.args[3], "/tmp/RawSpi.bin", "RawSpi.bin");
            requested_chain[id] = obj;
            Monitor.Exit(requested_chain);

            tftP_state = tftp_srtatus.in_progress;

            ret = waitForTftpDone();

            if (ret)
                return "/tmp/RawSpi.bin";
            else
                return string.Empty;

        }



        private string  UpLoadFile(Cube_command_reqObj req_obj)
        {
            //if (req_obj.cmdType == commandType.cmd_fmw_update_spi
                /*Put rest of checks as well)*/

            
                switch (req_obj.args[2])
                {
                    case "scp":
                        return DoScp(req_obj);
                        
                    case "cgi":
                        return DoCgi(req_obj);
                        
                    case "tftp":
                        return Dotftp(req_obj);
                        
                }

            
            
            return string.Empty;
        }



       
        private void handle_fmwUpdate(Cube_command_reqObj req_obj)
        {
            int id;
            string path = string.Empty;
            
            //Cube_command_reqObj obj = new Cube_command_reqObj(req_obj);
            /*
            if (req_obj.args[0] == "MrcPs171")
            {

                string file = localDir + "\\bank2_cmd_1";
                    KpScpSshclt.kp_scp(mIp, file, "/tmp/bank2_cmd_1", dyKI);
                    file = localDir + "\\bank2_cmd_2";
                    KpScpSshclt.kp_scp(mIp, file, "/tmp/bank2_cmd_2", dyKI);
            }
            else if (req_obj.args[0] == "MrcPs171")
            { 
              //take latest code and add bank info
            }
            */

            path = UpLoadFile(req_obj);

            if (path != string.Empty)
            {
                Monitor.Enter(requested_chain);
                id = AllCommands.updateFirmware(req_obj.args[0], path, "bank2");
                requested_chain[id] = req_obj;
                Monitor.Exit(requested_chain);
            }
        }
        /*
        private void handle_Op_state(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.SetOperationState(req_obj.args[0]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
            
        }
         * */
        private void handle_Op_state(Cube_command_reqObj req_obj)
        {
            int id;
            if (CheckFmwVersion() >= 0.54)
            {
                Monitor.Enter(requested_chain);
                id = AllCommands.SetOperationState(req_obj.args[0]);
                requested_chain[id] = req_obj;
                Monitor.Exit(requested_chain);
            }
            else
            {
                if (req_obj.args[0] != "reboot")
                {
                    MessageBox.Show("Only Reboot is posible with this versio");
                    return;
                }
                Monitor.Enter(requested_chain);
                id = AllCommands.SetReboot();
                requested_chain[id] = req_obj;
                Monitor.Exit(requested_chain);
            }

        }


        private void handle_Get_inputSeleted(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetMercurySelectedSource();
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_inputSeleted(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.SetMercurySourceType(req_obj.args[0]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_Activeinput(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetMercuryActiveSource();
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }


        private void handle_Get_geometry(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            
            id = AllCommands.GetWallGeometry(req_obj.args[0]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_RedGain(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);

            id = AllCommands.GetRedGain();
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_RedGain(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);

            id = AllCommands.SetRedGain(Convert.ToInt32(req_obj.args[0]));
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }


        private void handle_Get_GreenGain(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);

            id = AllCommands.GetGreenGain();
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_GreenGain(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);

            id = AllCommands.SetGreenGain(Convert.ToInt32(req_obj.args[0]));
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_BlueGain(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);

            id = AllCommands.GetBlueGain();
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }


        private void handle_Get_ModuleVersion(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetModuleVersion(req_obj.args[0]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_MacAdress(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetMacAddress(req_obj.args[0]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_fpga_reg(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.Getfpga_reg(req_obj.args[0]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }


        private void handle_Get_GenLockStatus(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGetLockStatus();
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_Channel_timing(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetInputTiming(req_obj.args[0]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_VideoByPass(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetVideoByPass(req_obj.args[0]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_rpcStatus(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.Getfmw_update_status(req_obj.args[0]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_runtimes(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGeneric_SysMgr("get_run_times");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }


        private void handle_Get_memInfo(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGeneric_SysMgr("get_mem_info");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_loadInfo(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGeneric_SysMgr("get_load_info");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_get_auto_save(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGeneric_SysMgr("get_auto_save");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }
        private void handle_get_ops_Operation_state(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGeneric_SysMgr("get_ops_operation");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);

            
        }

        private void handle_get_OP_state(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGeneric_SysMgr("get_device_operation");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);


        }

        private void handle_get_fmw_version(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetModuleVersion("MrcSpiProj");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);


        }

        private void handle_get_net_info(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetNetworkIf(req_obj.args[0]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
            
        }
        private void handle_get_net_info_ifCOnfig(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetNetInfo(req_obj.args[0]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }


        private void handle_get_cs_raw_count(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.Get_CSRaw_count_();
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }


        private void handle_get_cs_CIS_MEs(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.Get_CSRaw_mes_cis();
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_get_cs_CIS_BlackLIght_MEs(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.Get_CS_backLIght_get();
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }
        private void handle_get_cs_CIS_Update_BlackLIght_MEs(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.Get_CS_backLIght_update_get();
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_get_cs_raw_params(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGeneric_ColorSensor("get_cs_params");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_get_ops_state(Cube_command_reqObj req_obj)
        {

            int id = 1;
            Monitor.Enter(requested_chain);
            switch(req_obj.cmdType)
            {
                case commandType.cmd_ops_plug_state_get:
                    id = AllCommands.GetGeneric_SysMgr("get_ops_plug_state");
                break;
                case commandType.cmd_ops_fan_state_get:
                id = AllCommands.GetGeneric_SysMgr("get_ops_fan_state");
                break;
                case commandType.cmd_ops_power_state_get:
                id = AllCommands.GetGeneric_SysMgr("get_ops_power_state");
                break;

            }
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

       

        private void handle_Get_img_Errors(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGeneric_input("get_videoBypass_wallCfg_error");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_bid(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetBid(req_obj.args[0], req_obj.args[1]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_MstarFmwVersion(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetMstarFmwVersion();
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_Temprature(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetTemprature(req_obj.args[0]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_fanspeed(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetFanSpeed(req_obj.args[0]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }


        private void handle_Set_fanSpeed(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.SetFanSpeed(req_obj.args[0], Convert.ToInt32(req_obj.args[1]));
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_autoSave(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.SetAutoSave(req_obj.args[0]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }
        private void handle_Set_ops_Operation(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.Setops_Operation(req_obj.args[0]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_Cs_paras(Cube_command_reqObj req_obj)
        {

            int id;
            int gain = Convert.ToInt32(req_obj.args[0]);
            int prescale = Convert.ToInt32(req_obj.args[1]);

            Monitor.Enter(requested_chain);
             id = AllCommands.SetColorSensor_params(gain, prescale, req_obj.args[2], req_obj.args[3]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_Cs_LoopInterval(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.SetColor_sensor_Mes_Loop_time(req_obj.args[0], req_obj.args[1]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_CBM_mode(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.Set_cbm_mode(req_obj.args[0]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_White_balance(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.Set_white_Balance(Convert.ToDouble(req_obj.args[0]),
                                                    Convert.ToDouble(req_obj.args[1]),
                                                    Convert.ToDouble(req_obj.args[2])
                                                    );
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_cbm_mode_params(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.Set_cbm_params(Convert.ToInt32(req_obj.args[0]),
                                        Convert.ToInt32(req_obj.args[1]),
                                        Convert.ToInt32(req_obj.args[2]),
                                        Convert.ToInt32(req_obj.args[3]),
                                        Convert.ToInt32(req_obj.args[4]),
                                        Convert.ToInt32(req_obj.args[5]),
                                        Convert.ToInt32(req_obj.args[6]));

            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }


        private void handle_Set_user_target_params(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.Set_user_targetControl_data(Convert.ToInt32(req_obj.args[0]),
                                                        Convert.ToDouble(req_obj.args[1]),
                                                        req_obj.args[2],
                                                        Convert.ToInt32(req_obj.args[3])
                                                        );
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_Cs_back_light(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.SetBackLight_via_cs(Convert.ToInt32(req_obj.args[0]));
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_Cs_back_light(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetBackLight_Via_cs();
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_user_target_control(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGeneric_ColorSensor("get_user_target_control_params");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_cbm_mode(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGeneric_ColorSensor("get_cbm_mode");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_white_balance(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGeneric_ColorSensor("get_white_balance");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_CBM_NativeMode_params(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGeneric_ColorSensor("get_cbm_native_params");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_cs_gobal_errors(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGeneric_ColorSensor("get_status_err_info");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_cs_Min_max_brightness(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGeneric_ColorSensor("max_min_brightness");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Tigger_white_correction(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGeneric_ColorSensor("trigger_white_correction");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_filed_calibrations(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);

            if(req_obj.args[0] == "cs0")
                id = AllCommands.GetGeneric_ColorSensor("get_field_calibration_cs0");
            else
                id = AllCommands.GetGeneric_ColorSensor("get_field_calibration_cs1");

            requested_chain[id] = req_obj;

            Monitor.Exit(requested_chain);
        }

        private void handle_reset_filed_calibrations(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);

            if (req_obj.args[0] == "cs0")
                id = AllCommands.ReSetField_calibration("set_field_calibration_cs0");
            else
                id = AllCommands.ReSetField_calibration("set_field_calibration_cs1");

            requested_chain[id] = req_obj;

            Monitor.Exit(requested_chain);
        }

        private void handle_Get_factory_calibrations(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);

            if (req_obj.args[0] == "cs0")
                id = AllCommands.ReSetField_calibration("get_factory_calibration_cs0");
            else
                id = AllCommands.ReSetField_calibration("get_factory_calibration_cs1");

            requested_chain[id] = req_obj;

            Monitor.Exit(requested_chain);
        }

        private void handle_Get_ext_field_ref_data(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGeneric_ColorSensor("get_external_field_cs_ref_data");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_ext_factory_ref_data(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGeneric_ColorSensor("get_external_factory_cs_ref_data");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_XYZ_Calib_data(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetCalib_XYZ_device(req_obj.args[0]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_Pattren_type(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGeneric_input("get_pattern");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_Gaam_upload_sts(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGeneric_SysMgr("get_barco_gamma_upload_sts");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }
        
        private void handle_Get_fna_ticks(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetFanTicks(req_obj.args[0]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_LogMsg_count(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGeneric_SysMgr("get_logmsg_count");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_LogMsgs(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetLogMessage(req_obj.linenum.ToString());
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_Diagnostic_state(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGeneric_SysMgr("get_diagnosis_result");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_Diagnostic_File(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            //id = AllCommands.GetDianosticMessage(req_obj.linenum.ToString());
            id = AllCommands.GetDianosticMessage(req_obj.args[0]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_Color_Offsets(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            
            if (req_obj.cmdType == commandType.cmd_pic_red_offset_get)
            id = AllCommands.GetRedOffset();
            else if (req_obj.cmdType == commandType.cmd_pic_green_offset_get)
                id = AllCommands.GetGreenOffset();
            else
                id = AllCommands.GetBlueOffset();

            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_Diagnostic_Count(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGeneric_SysMgr("get_diagnosis_result_count");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }


        private void handle_Get_int_Field_factory_ref_data(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            if (req_obj.cmdType == commandType.cmd_cs_int_field_ref_data_get)
                id = AllCommands.GetGeneric_ColorSensor("get_internal_field_cs_ref_data");
            else
                id = AllCommands.GetGeneric_ColorSensor("get_internal_factory_cs_ref_data");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_int_Field_factory_ref_data(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            if (req_obj.cmdType == commandType.cmd_cs_int_field_ref_data_set)
                id = AllCommands.GetGeneric_ColorSensor("set_internal_field_cs_ref_data");
            else
                id = AllCommands.GetGeneric_ColorSensor("set_internal_factory_cs_ref_data");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_Cube_blink_identify(Cube_command_reqObj req_obj)
        {
          
            
            int id;
            Monitor.Enter(requested_chain);
                id = AllCommands.SetGeneric_SysMgr("set_device_identify");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }


        private void handle_Set_Pattren(Cube_command_reqObj req_obj)
        {


            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.SetPattern(req_obj.args[0]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_barco_gama(Cube_command_reqObj req_obj)
        {


            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGeneric_SysMgr("upload_barco_gamma");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_Log_flags(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.Set_detail_log(req_obj.args[0]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_Trigger_diagnostics(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetGeneric_SysMgr("trigger_auto_diagnosis");
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_Wall_geometry(Cube_command_reqObj req_obj)
        {
            int id;
            int x, y, xmax, ymax;
            x = Convert.ToInt32(req_obj.args[0]);
            y = Convert.ToInt32(req_obj.args[1]);
            xmax = Convert.ToInt32(req_obj.args[2]);
            ymax = Convert.ToInt32(req_obj.args[3]);

            Monitor.Enter(requested_chain);
            id = AllCommands.SetWallGeometry(x, y, xmax, ymax, req_obj.args[4], req_obj.args[5]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_do_artificial_sleep(Cube_command_reqObj req_obj)
        {
            Thread.Sleep(Int32.Parse(req_obj.args[0]));
            
        }

        private void handle_Set_ext_factory_ref_data(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.Set_External_Ref_data(req_obj.args[0],
                                                    req_obj.args[1],
                                                    req_obj.args[2],
                                                    req_obj.args[3],
                                                    Convert.ToDouble(req_obj.args[4]),
                                                    Convert.ToDouble(req_obj.args[5]),
                                                    Convert.ToDouble(req_obj.args[6]),
                                                    Convert.ToDouble(req_obj.args[7]),
                                                    Convert.ToDouble(req_obj.args[8]),
                                                    Convert.ToDouble(req_obj.args[9]),
                                                    Convert.ToDouble(req_obj.args[10]),
                                                    Convert.ToDouble(req_obj.args[11]),
                                                    Convert.ToDouble(req_obj.args[12])
                                                        );
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_Black_while_level(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);
            
            if (req_obj.cmdType == commandType.cmd_cs_black_level_get)
                id = AllCommands.GetGeneric_ColorSensor("get_black_level");
            else
                id = AllCommands.GetGeneric_ColorSensor("get_white_level");


            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_Black_while_level(Cube_command_reqObj req_obj)
        {
            int id;
            Monitor.Enter(requested_chain);

            if (req_obj.cmdType == commandType.cmd_cs_black_level_set)
                id = AllCommands.Set_BlackWhite_level("set_black_level", Convert.ToInt32(req_obj.args[0]));
            else
                id = AllCommands.Set_BlackWhite_level("set_white_level", Convert.ToInt32(req_obj.args[0]));
                


            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_net_if(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.SetNetworkIf(req_obj.args[0],
                                          req_obj.args[1],
                                          req_obj.args[2],
                                          req_obj.args[3]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);


            

        }

        private void handle_Set_VideoBypass(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.SetVideoByPass(req_obj.args[0], req_obj.args[1]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_macAdress(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.SetMacAddress(req_obj.args[0],req_obj.args[1]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_fpga_reg(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.Setfpga_reg(int.Parse(req_obj.args[0]), int.Parse(req_obj.args[1]));
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_bid(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.Setbid(req_obj.args[0], req_obj.args[1], req_obj.args[2]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_edid(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);

            if (req_obj.args[1] == string.Empty) //Custom edid
            {
                KpScpSshclt.kp_scp(mIp, req_obj.args[2], "/tmp/cutomEdit.bin", GetDyki());
                id = AllCommands.Setedid(req_obj.args[0], "custom", "/tmp/cutomEdit.bin");
            }
            else
                id = AllCommands.Setedid(req_obj.args[0], "custom", req_obj.args[1]);
            
                      
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Verify_edid(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);

            if (req_obj.args[1] == string.Empty) //Custom edid
            {
                KpScpSshclt.kp_scp(mIp, req_obj.args[2], "/tmp/cutomEdit.bin", GetDyki());
                id = AllCommands.Verifyedid(req_obj.args[0],  "/tmp/cutomEdit.bin");
            }
            else
                id = AllCommands.Verifyedid(req_obj.args[0], req_obj.args[1]);


            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }


        private void handle_Set_Mercury_video(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.SetMstarSourceShow();
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_SaveAll(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.saveALL();
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_BlueGain(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);

            id = AllCommands.SetBlueGain(Convert.ToInt32(req_obj.args[0]));
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }


        private void handle_Get_brightness(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);

            id = AllCommands.GetBrightness();
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_backlightboost(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);

            id = AllCommands.SetBackLightBoost(req_obj.args[0]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_brightness(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);

            id = AllCommands.SetBrightness(Convert.ToInt32(req_obj.args[0]));
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }


        private void handle_Get_backlight(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);

            id = AllCommands.GetBackLight();
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_backlight(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);

            id = AllCommands.SetBackLight(Convert.ToInt32(req_obj.args[0]));
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_Contrast(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);

            id = AllCommands.GetContrast();
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_Contrast(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);

            id = AllCommands.SetContrast(Convert.ToInt32(req_obj.args[0]));
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }


        private void handle_Get_OpticalCable(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetDviOptical(req_obj.args[0]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_OpticalCable(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.SetDviOptical(req_obj.args[0], req_obj.args[1]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_background(Cube_command_reqObj req_obj)
        {

            int id;
            int r = Convert.ToInt32(req_obj.args[1]);
            int g = Convert.ToInt32(req_obj.args[2]);
            int b = Convert.ToInt32(req_obj.args[3]);
            Monitor.Enter(requested_chain);
            id = AllCommands.SetBackgroundColor(req_obj.args[0], r,g,b);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Set_Geometry_state(Cube_command_reqObj req_obj)
        {

            int id;
            
            Monitor.Enter(requested_chain);
            id = AllCommands.SetWallFullScrean(req_obj.args[0]);
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);

        }


        private void handle_Get_Background(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetBackgroundColor();
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }

        private void handle_Get_wallgeometryStatus(Cube_command_reqObj req_obj)
        {

            int id;
            Monitor.Enter(requested_chain);
            id = AllCommands.GetWallFullScrean();
            requested_chain[id] = req_obj;
            Monitor.Exit(requested_chain);
        }
        
        private void process_req(Cube_command_reqObj req_obj)
        {
            
            switch (req_obj.cmdType)
            {
                
                case commandType.cmd_fmw_update_spi:
                case commandType.cmd_fmw_update_mstar:
                case commandType.cmd_fmw_update_ps171:
                
                //case commandType.cmd_fmw_update_mmc:
                     handle_fmwUpdate(req_obj);
                    break;
                case commandType.cmd_gen_operation_state_set:
                case commandType.cmd_gen_operation_state_StandBY_set:
                case commandType.cmd_gen_operation_state_reboot_set:
                    handle_Op_state(req_obj);
                    break;

                case commandType.cmd_get_input_selected:
                case commandType.cmd_get_input_selected_internal:
                    handle_Get_inputSeleted(req_obj);
                    break;
                case commandType.cmd_get_Active_input:
                    handle_Get_Activeinput(req_obj);
                    break;
                case commandType.cmd_set_input_selected:
                    handle_Set_inputSeleted(req_obj);
                    break;
                case commandType.cmd_img_wallGeometry_get:
                    handle_Get_geometry(req_obj);
                    break;
                case commandType.cmd_pic_contrst_get:
                    handle_Get_Contrast(req_obj);
                    break;
                case commandType.cmd_pic_contrst_set:
                    handle_Set_Contrast(req_obj);
                    break;
                case commandType.cmd_optical_dvi_get:
                    handle_Get_OpticalCable(req_obj);
                    break;
                case commandType.cmd_optical_dvi_set:
                    handle_Set_OpticalCable(req_obj);
                    break;
                case commandType.cmd_img_background_get:
                    handle_Get_Background(req_obj);
                    break;
                case commandType.cmd_img_background_set:
                    handle_Set_background(req_obj);
                    break;
                case commandType.cmd_img_wallConfig_state_get:
                    handle_Get_wallgeometryStatus(req_obj);
                    break;
                case commandType.cmd_img_wallConfig_state_set:
                    handle_Set_Geometry_state(req_obj);
                    break;
                case commandType.cmd_pic_backlight_get:
                    handle_Get_backlight(req_obj);
                    break;
                case commandType.cmd_pic_backlight_set:
                    handle_Set_backlight(req_obj);
                    break;
                case commandType.cmd_pic_brightness_get:
                    handle_Get_brightness(req_obj);
                    break;
                case commandType.cmd_pic_brightness_set:
                    handle_Set_brightness(req_obj);
                    break;
                case commandType.cmd_pic_boostLight_set:
                    handle_Set_backlightboost(req_obj);
                    break;
                case commandType.cmd_pic_red_gain_get:
                    handle_Get_RedGain(req_obj);
                    break;
                case commandType.cmd_pic_red_gain_set:
                    handle_Set_RedGain(req_obj);
                    break;
                case commandType.cmd_pic_green_gain_get:
                    handle_Get_GreenGain(req_obj);
                    break;
                case commandType.cmd_pic_green_gain_set:
                    handle_Set_GreenGain(req_obj);
                    break;
                case commandType.cmd_pic_blue_gain_get:
                    handle_Get_BlueGain(req_obj);
                    break;
                case commandType.cmd_pic_blue_gain_set:
                    handle_Set_BlueGain(req_obj);
                    break;
                case commandType.cmd_mo_get_fan_speed:
                    handle_Get_fanspeed(req_obj);
                    break;
                case commandType.cmd_mo_get_fan_speed_set:
                    handle_Set_fanSpeed(req_obj);
                    break;
                case commandType.cmd_temprature_get:
                    handle_Get_Temprature(req_obj);
                    break;

                case commandType.cmd_module_version_get:
                    handle_Get_ModuleVersion(req_obj);
                    break;
                case commandType.cmd_mac_address_get:
                    handle_Get_MacAdress(req_obj);
                    break;
                case commandType.cmd_mac_address_set:
                    handle_Set_macAdress(req_obj);
                    break;
                case commandType.cmd_fmw_mstar_version_get:
                    handle_Get_MstarFmwVersion(req_obj);
                    break;
                case commandType.cmd_fpga_reg_set:
                    handle_Set_fpga_reg(req_obj);
                    break;
                case commandType.cmd_fpga_reg_get:
                    handle_Get_fpga_reg(req_obj);
                    break;

                case commandType.cmd_bid_get:
                    handle_Get_bid(req_obj);
                    break;
                case commandType.cmd_bid_set:
                    handle_Set_bid(req_obj);
                    break;
                case commandType.cmd_dataBase_saveAll:
                    handle_SaveAll(req_obj);
                    break;
                case commandType.cmd_img_genlock_status_get:
                    handle_Get_GenLockStatus(req_obj);
                    break;
                case commandType.cmd_img_input_timing_get:
                    handle_Get_Channel_timing(req_obj);
                    break;
                case commandType.cmd_img_errors_get:
                    handle_Get_img_Errors(req_obj);
                    break;
                case commandType.cmd_img_mstar_video_set:
                    handle_Set_Mercury_video(req_obj);
                    break;
                case commandType.cmd_edid_set:
                    handle_Set_edid(req_obj);
                    break;
                case commandType.cmd_edid_verify:
                    handle_Verify_edid(req_obj);
                    break;
                case commandType.cmd_img_video_byPass_get:
                    handle_Get_VideoByPass(req_obj);
                    break;
                case commandType.cmd_img_video_byPass_set:
                    handle_Set_VideoBypass(req_obj);
                    break;
                case commandType.cmd_fmw_update_status_get:
                case commandType.cmd_rpc_status_get:
                case commandType.cmd_cs_barco_gama_set_update_status:
                case commandType.cmd_SaveAll_status_get:
                case commandType.cmd_mo_tigger_Diagnostics_status_get:
                    handle_Get_rpcStatus(req_obj);
                    break;
                case commandType.cmd_mo_runtimes_get:
                    handle_Get_runtimes(req_obj);
                    break;
                case commandType.cmd_mo_meminfo_get:
                    handle_Get_memInfo(req_obj);
                    break;
                case commandType.cmd_mo_loadinfo_get:
                    handle_Get_loadInfo(req_obj);
                    break;
                case commandType.cmd_AutoSave_state_get:
                    handle_get_auto_save(req_obj);
                    break;
                case commandType.cmd_AutoSave_state_set:
                    handle_Set_autoSave(req_obj);
                    break;
                case commandType.cmd_ops_power_state_get:
                case commandType.cmd_ops_fan_state_get:
                case commandType.cmd_ops_plug_state_get:
                    handle_get_ops_state(req_obj);
                    break;
                case commandType.cmd_ops_operationt_get:
                    handle_get_ops_Operation_state(req_obj);
                    break;
                case commandType.cmd_ops_operationt_set:
                    handle_Set_ops_Operation(req_obj);
                    break;
                case commandType.cmd_gen_operation_state_get:
                    handle_get_OP_state(req_obj);
                    break;
                case commandType.cmd_mrc_spi_module_version_get:
                    handle_get_fmw_version(req_obj);
                    break;
                case commandType.cmd_net_networkIf_get:
                    handle_get_net_info(req_obj);
                    break;
                case commandType.cmd_net_networkIf_set:
                    handle_Set_net_if(req_obj);
                    break;
                case commandType.cmd_net_NetInfo_get:
                    handle_get_net_info_ifCOnfig(req_obj);
                    break;

                case commandType.cmd_cs_raw_count_get:
                    handle_get_cs_raw_count(req_obj);
                    break;
                case commandType.cmd_cs_raw_mes_cis_get:
                    handle_get_cs_CIS_MEs(req_obj);
                    break;

                case commandType.cmd_cs_backlight_mes_get:
                    handle_get_cs_CIS_BlackLIght_MEs(req_obj);
                    break;
                case commandType.cmd_cs_backlight_mes_update_get:
                    handle_get_cs_CIS_Update_BlackLIght_MEs(req_obj);
                    break;
                case commandType.cmd_cs_params_get:
                    handle_get_cs_raw_params(req_obj);
                    break;
                case commandType.cmd_cs_params_set:
                    handle_Set_Cs_paras(req_obj);
                    break;
                case commandType.cmd_cs_loop_time_set:
                    handle_Set_Cs_LoopInterval(req_obj);
                    break;
                case commandType.cmd_cs_backLight_set:
                    handle_Set_Cs_back_light(req_obj);
                    break;
                case commandType.cmd_cs_backLight_get:
                    handle_Get_Cs_back_light(req_obj);
                    break;
                case commandType.cmd_cs_user_target_control_params_get:
                    handle_Get_user_target_control(req_obj);
                    break;
                case commandType.cmd_cs_user_target_control_params_set:
                    handle_Set_user_target_params(req_obj);
                    break;
                case commandType.cmd_cs_cbm_mode_get:
                    handle_Get_cbm_mode(req_obj);
                    break;
                case commandType.cmd_cs_cbm_mode_set:
                    handle_Set_CBM_mode(req_obj);
                    break;
                case commandType.cmd_cs_white_banance_get:
                    handle_Get_white_balance(req_obj);
                    break;
                case commandType.cmd_cs_white_banance_set:
                    handle_Set_White_balance(req_obj);
                    break;
                case commandType.cmd_cs_white_level_get:
                case commandType.cmd_cs_black_level_get:
                    handle_Get_Black_while_level(req_obj);
                    break;
                case commandType.cmd_cs_white_level_set:
                case commandType.cmd_cs_black_level_set:
                    handle_Set_Black_while_level(req_obj);
                    break;
                case commandType.cmd_cs_cbm_mode_params_get:
                    handle_Get_CBM_NativeMode_params(req_obj);
                    break;
                case commandType.cmd_cs_cbm_mode_params_set:
                    handle_Set_cbm_mode_params(req_obj);
                    break;
                case commandType.cmd_cs_gobal_error_info_get:
                case commandType.cmd_cs_gobal_error_info_get_internal:
                    handle_Get_cs_gobal_errors(req_obj);
                    break;
                case commandType.cmd_cs_max_min_brightness_get:
                    handle_Get_cs_Min_max_brightness(req_obj);
                    break;
                case commandType.cmd_cs_tigger_white_correction:
                    handle_Tigger_white_correction(req_obj);
                    break;
                case commandType.cmd_cs_field_calib_get:
                    handle_Get_filed_calibrations(req_obj);
                    break;
                case commandType.cmd_cs_field_calib_reset:
                    handle_reset_filed_calibrations(req_obj);
                    break;
                case commandType.cmd_cs_factory_calib_get:
                    handle_Get_factory_calibrations(req_obj);
                    break;
                case commandType.cmd_cs_ext_field_ref_data_get:
                    handle_Get_ext_field_ref_data(req_obj);
                    break;
                case commandType.cmd_cs_ext_factory_ref_data_get:
                    handle_Get_ext_factory_ref_data(req_obj);
                    break;
                case commandType.cmd_cs_ext_field_ref_data_set:
                case commandType.cmd_cs_ext_factory_ref_data_set:
                    handle_Set_ext_factory_ref_data(req_obj);
                    break;
                case commandType.cmd_cs_int_factory_ref_data_get:
                case commandType.cmd_cs_int_field_ref_data_get:
                    handle_Get_int_Field_factory_ref_data(req_obj);
                    break;
                case commandType.cmd_cs_int_field_ref_data_set:
                case commandType.cmd_cs_int_factory_ref_data_set:
                    handle_Set_int_Field_factory_ref_data(req_obj);
                    break;
                case commandType.cmd_cs_Calibrated_XYZ_get:
                    handle_Get_XYZ_Calib_data(req_obj);
                    break;
                case commandType.cmd_cube_Blink_identify:
                case commandType.cmd_cube_Blink_identify_assign_mode:
                    handle_Set_Cube_blink_identify(req_obj);
                    break;
                case commandType.cmd_img_pattern_type_get:
                    handle_Get_Pattren_type(req_obj);
                    break;
                case commandType.cmd_img_pattern_type_set:
                    handle_Set_Pattren(req_obj);
                    break;
                case commandType.cmd_cs_barco_gama_set:
                    handle_Set_barco_gama(req_obj);
                    break;
                case commandType.cmd_cs_gaam_upload_status_get:
                    handle_Get_Gaam_upload_sts(req_obj);
                    break;
                case commandType.cmd_mo_get_fan_ticks:
                    handle_Get_fna_ticks(req_obj);
                    break;
                case commandType.cmd_mo_get_log_msg_count_internal:
                    handle_Get_LogMsg_count(req_obj);
                    break;
                case commandType.cmd_mo_get_log_msg_internal:
                    handle_Get_LogMsgs(req_obj);
                    break;
                case commandType.cmd_img_Detail_Log_flag_set:
                    handle_Set_Log_flags(req_obj);
                    break;
                case commandType.cmd_mo_tigger_Diagnostics:
                    handle_Set_Trigger_diagnostics(req_obj);
                    break;
                case commandType.cmd_diagnostics_state_get:
                    handle_Get_Diagnostic_state(req_obj);
                    break;
                case commandType.cmd_mo_Get_Diagnostics_count_get:
                    handle_Get_Diagnostic_Count(req_obj);
                    break;
                case commandType.cmd_mo_Get_Diagnostics_msgs_get:
                    handle_Get_Diagnostic_File(req_obj);
                    break;
                case commandType.cmd_img_wallGeometry_set:
                    handle_Set_Wall_geometry(req_obj);
                    break;
                case commandType.cmd_pic_red_offset_get:
                case commandType.cmd_pic_blue_offset_get:
                case commandType.cmd_pic_green_offset_get:
                    handle_Get_Color_Offsets(req_obj);
                    break;
                case commandType.cmd_artificial_sleep_set:
                    handle_do_artificial_sleep(req_obj);
                    break;




                //cmd_artificial_sleep_set
                    
                    
                
                
                default:
                    MessageBox.Show(req_obj.cmdType.ToString() + " Command is not supported");
                 break;
            
            }

        
        }






    }
}
